<template>
  <view class="contain">
    <view v-if="!isRefresh" :class="refreshClass" class="animate fixed-top h-46 flex  box-c"
          :style="{ 'padding-top': top + 'px','background-color': style['discovery_nav_color'],'color': style['discovery_nav_font_color'] }">
            <view @click="back(1)"
                  class="flex f-s-0 padding-lr10 f-a-c f-j-c van-icon van-icon-arrow-left f20-size"></view>
      <view class="flex flex-1 t-color-w">

        <view :class="active == 0 ? 'f-w-b f18-size' : 'f16-size'" class="flex f-a-c f-j-c w-50">{{ i18n['发现'] }}
        </view>
        <view @click="goToVideo" :class="active == 1 ? 'f-w-b f18-size' : 'f16-size'" class="flex f-a-c f-j-c w-50">
          {{ i18n['视频'] }}
        </view>

      </view>
      <view class="flex f-s-0">

      </view>
      <xcx-header></xcx-header>
    </view>
    <view :style="{ 'padding-top': top +46+10+ 'px' }"></view>

    <view class="padding-6">
      <find-list :location="3" :logo="logo" :list="list" :offset='1.2' :modeType="modeType" :isGg="true" ref="findList" :isOnlyOne="true"></find-list>
      <u-loadmore :status="status" v-if="list.length>0&&total>pageSize"/>
      <no-data :list="list"></no-data>
    </view>
    <zero-loading v-if="loading" :mask="true" type="sword"></zero-loading>

    <toUserAuth ref="toUserAuth"
                :invitMsg="invitMsg"
                :isDisabled="isDisabled"
                :invitationCode="invitationCode"
                :currentDoAuth="currentDoAuth"
                @indexGgRefresh="indexGgRefresh"></toUserAuth>
    <notice></notice>
  </view>
</template>
<style scoped>
@import url('../../static/css/index/index.css');
</style>

<script>
import toUserAuth from './../user/components/toUserAuth.vue';

import findList from './components/findlist.vue';
import login from "@/pagesB/passport/login.vue";

const API = require('../../utils/api/find.js').default;
const $ = require('../../utils/api.js');
let self;
export default {
  data() {
    return {
      logo:"",
      loading: false,
      status: '',
      // modeType: "widthFix",
      modeType: "scaleToFill",
      top: uni.getStorageSync('bartop') ? uni.getStorageSync('bartop') : 0,
      isIphonex: uni.getStorageSync('isIphonex') ? uni.getStorageSync('isIphonex') : false,
      active: 0,
      list: [],
      page: 1,
      total:0,
      pageSize: 6,
      totalPage: 1,
      isRefresh: false,
      refreshClass: [''],
      style: '',
      invitMsg: "商家邀请码：",
      isDisabled: true,
      invitationCode: "",
      currentDoAuth: 'add'

    };
  },
  onLoad: function () {
    self = this;
    self.init();
  },
  onShow() {

  },
  methods: {
    indexGgRefresh() {
      this.$emit("indexRefresh")
    },
    toScan() {
      let isLogin = uni.getStorageSync('token') ? true : false;
      /*#ifdef H5*/
      $.$toast("当前为H5页面暂不支持扫一扫，请移步小程序");
      /*#endif*/

      /*#ifdef MP*/
      let self = this
      uni.scanCode({
        success: function (res) {
          // $.$toast('条码类型：' + res.scanType + "," + '条码内容：' + res.result);
          //扫码成功后如果内容中的扫码类型是认证，就打开认证窗口
          var n = res.result.replace(/\ufeff/g, "")
          let jsonResult = JSON.parse(n)
          if (jsonResult.scanType === "authentication") {
            if (isLogin) {
              self.initToUserAuth(jsonResult.authFromInvitCode, jsonResult.shopName);
            } else {
              $.$toast("请登录后进行认证");
            }

          }

        }
      });
      /*#endif*/

    },
    initToUserAuth(authFromInvitCode, shopName) {
      this.$refs.toUserAuth.init(null, null, null, null, null, authFromInvitCode, shopName)
    },
    goToVideo() {
      if (this.list.length <= 0) {
        $.$toast("暂无视频");
      } else {
        $.go('/pagesA/find/video?isGg=true&location=3')
      }

    },
    refreshNext() {
      this.$refs.findList.refreshNext()
    },
    getList() {
      const self = this;
      self.loading = true
      $.ajax({
        url: API.searchOriginalMaterials,
        data: {
          type: '',
          page: self.page,
          pageSize: self.pageSize
        },
        method: 'GET',
        success(res) {
          self.loading = false

          let list = res.data.list ? res.data.list : [];
          if (list.length <= 0 && self.page == 1) {
            self.list = [];
            return;
          }
          list.forEach((ele, index) => {
            let obj = ele.walkTrends;
            obj.collectTrends = ele.collectTrends;
            obj.collectWalkMember = ele.collectWalkMember;
            obj.index = index;
            if (obj.videoUrl && obj.videoUrl != '') {
              obj['mainImg'] = obj.videoUrl + "?" +uni.getStorageSync('imageProcessingSuffix');
            } else if (String(obj.images).indexOf('|') >= 0) {
              obj['mainImg'] = obj.images.split('|')[0];
            } else {
              obj['mainImg'] = obj.images;
            }
            self.list.push(obj);
          })
          self.totalPage = res.data.totalPage;
          self.total = res.data.total;
          let pageNum = res.data.pageNum;
          if (pageNum==self.totalPage){
            self.status='nomore'
          }else{
            self.status='loadmore'
          }
          // #ifdef MP-WEIXIN
          if (self.isRefresh) {
            self.isRefresh = false;
            self.refreshClass = ['fadeIn'];
          }
          // #endif
          uni.stopPullDownRefresh();
        }
      })
    },
    init() {
      let self = this
      self.logo = uni.getStorageSync('config').logo
      self.page = 1;
      self.list = [];
      self.getList();
      self.getStyle().then((res) => {
        self.style = res;
      })
    },
  },
  computed: {
    i18n() {
      return this.$t('find')
    },
  },
  created() {
  },
  mounted() {
  },
  destroyed() {
  },
  components: {findList, toUserAuth},
  onPullDownRefresh() {
    // // #ifdef MP-WEIXIN
    // this.isRefresh = true;
    // this.refreshClass = ['fadeOut'];
    // // #endif
    // this.init();
    // //重新刷新下子组件
    this.page = 1;
    this.list = []
    this.getList();
    this.$refs.findList.refreshNext()
  },
  onReachBottom() {
    if (this.page < this.totalPage) {
      this.status = 'loading'
      this.page += 1;
      this.getList();
    }
  }
}
</script>
